/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 * 
 * Contact luchyx@riaevolution.com
 * See www.riaevolution.com
 *  
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 * */
package com.riaevolution.components
{
	import mx.containers.HBox;
	import mx.controls.TextInput;
	import mx.utils.StringUtil;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import mx.controls.Alert;

	public class InLinePhoneInput extends HBox
	{
		
		public static var Expresion:RegExp = /\((\d{2,5})\)/;
		private var area_ti:TextInput;
		private var number_ti:TextInput;

		private var _area:String = "";
		private var _number:String = "";
		private var _internal_num:String = "";
		
		
		private var changePhonePending:Boolean = false;		
		private var bAreaUpdateP:Boolean = false;
		private var bPhoneUpdateP:Boolean = false;
		
		private var intAreaUpdateP:Boolean = false;
		private var intPhoneUpdateP:Boolean = false;
		
		public function InLinePhoneInput()
		{
			//TODO: implement function
			super();
		}
		
		override protected function createChildren():void {
			super.createChildren();
			if(!area_ti) {
				area_ti = new TextInput();
				area_ti.width = 48;
				area_ti.restrict = "0-9";
				area_ti.toolTip = 'Ingrese el código de localidad';
				area_ti.addEventListener(FocusEvent.FOCUS_OUT, updateProp);
				addChild(area_ti);
			}
			if(!number_ti) {
				number_ti = new TextInput();
				number_ti.width = 84;
				number_ti.restrict = "0-9\\-";
				number_ti.toolTip = 'Ingrese el número telefónico';
				number_ti.addEventListener(FocusEvent.FOCUS_OUT, updateProp);
				addChild(number_ti);
			}
		}
		/**
		 * Vamos a actualizar los componentes. 
		 *
		 */
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			
			if(changePhonePending) {
				changePhonePending = false;
				updateFormated();
			}
			if(bAreaUpdateP) {
				bAreaUpdateP = false;
				area_ti.text = _area;
			}
			if(bPhoneUpdateP) {
				bPhoneUpdateP = false;
				number_ti.text = _number;
			}
			if(intPhoneUpdateP) {
				intPhoneUpdateP = false;
				_number = number_ti.text;
				_internal_num = getFormated();
			}
			if(intAreaUpdateP) {
				intAreaUpdateP = false;
				_number = area_ti.text;
				_internal_num = getFormated();
			}
			
			super.updateDisplayList(unscaledWidth, unscaledHeight);
		}
		
		
				
		[Bindable]
		public function set AreaCode(s:String):void {
			_area = s;
			bAreaUpdateP = true;
		}
		public function get AreaCode():String {
			return _area;
		}
		
		[Bindable]
		public function set PhoneNumber(s:String):void {
			_number = s;
			bPhoneUpdateP = true;
		}
		public function get PhoneNumber():String {
			return _number;
		}
		
		[Bindable]
		public function set Phone(s:String):void {
			_internal_num = s;
			changePhonePending = true;
		}
		public function get Phone():String {
			return _internal_num;
		}
		
		private function updateFormated():void {
			var obj:Array = _internal_num.split(Expresion);
			if(obj[1]) {
				_area = obj[1];
				bAreaUpdateP = true;
			}
			if(obj[2]) {
				_number = StringUtil.trim(obj[2]);
				bPhoneUpdateP = true;
			}
		}
		private function getFormated():String {
			var s:String = "";
			if(AreaCode.length>1) {
				s = "("+AreaCode+") ";
			}
			s+=PhoneNumber;
			return s;
		}
		private function updateProp(e:Event):void {
			if(e.currentTarget == area_ti) {
				intAreaUpdateP = true;
			} else {
				intPhoneUpdateP = true;
			}
		}
	}
}